package com.liujie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liujie.common.R;
import com.liujie.entity.Orders;
import com.liujie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        boolean submit = ordersService.submit(orders);
        if (submit){
            return R.success("订单提交成功");
        }
        return R.error("订单提交失败");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.orderByAsc(Orders::getCheckoutTime);
        ordersService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime,String endTime){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.like(number!=null,Orders::getId,number)
                .between(beginTime!=null&&endTime!=null,Orders::getOrderTime,beginTime,endTime)
                .orderByAsc(Orders::getCheckoutTime);
        ordersService.page(pageInfo,lqw);
        return R.success(pageInfo);

    }

    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        boolean b = ordersService.updateById(orders);
        if (b){
            return R.success("订单状态已修改");
        }
        return R.error("状态修改失败");
    }
}
